package com.niguang.labour.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;

public class MillisecondUtil {

    private static final DecimalFormat numberFormat = new DecimalFormat("##.##");

    /**
     * 毫秒转时间 格式：HH:mm:ss
     *
     * @param milliSecondTime
     * @return
     */
    public static String millisecondToTime(Long milliSecondTime) {
        Long hour = milliSecondTime / (60 * 60 * 1000);
        Long minute = (milliSecondTime - hour * 60 * 60 * 1000) / (60 * 1000);
        Long seconds = (milliSecondTime - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000;

        if (seconds >= 60) {
            seconds = seconds % 60;
            minute += seconds / 60;
        }
        if (minute >= 60) {
            minute = minute % 60;
            hour += minute / 60;
        }

        String sh = "";
        String sm = "";
        String ss = "";
        if (hour < 10) {
            sh = "0" + String.valueOf(hour);
        } else {
            sh = String.valueOf(hour);
        }
        if (minute < 10) {
            sm = "0" + String.valueOf(minute);
        } else {
            sm = String.valueOf(minute);
        }
        if (seconds < 10) {
            ss = "0" + String.valueOf(seconds);
        } else {
            ss = String.valueOf(seconds);
        }

        return sh + ":" + sm + ":" + ss;
    }

    public static void main(String[] args) {
        System.out.println(millisecondToHour(11100000L));

    }


    /**
     * 毫秒转换成小时
     *
     * @param ms
     * @return
     */
    public static String millisecondToHour(Long ms) {
        if (ms == null || ms == 0L) {
            return "0";
        }
        String hour = numberFormat.format(ms / (60 * 60 * 1000.00));
        return String.valueOf(hour);
    }

    public static String changeTime(String millisecond) {
        if (StringUtils.isBlank(millisecond)) {
            return null;
        }
        long speedAllot = Math.round(Double.valueOf(millisecond));
        String time = millisecondToTime(speedAllot);
        String[] split = time.split(":");
        return split[1] + "分" + split[2] + "秒";
    }

    public static String changeHourTime(Long millisecond) {
        if (millisecond == null) {
            return "00时00分00秒";
        }
        long speedAllot = Math.round(Double.valueOf(millisecond));
        String time = millisecondToTime(speedAllot);
        String[] split = time.split(":");
        return split[0] + "时" + split[1] + "分" + split[2] + "秒";
    }

    public static String changeTime(Long millisecond) {
        if (millisecond == null) {
            return null;
        }
        long speedAllot = Math.round(Double.valueOf(String.valueOf(millisecond)));
        String time = millisecondToTime(speedAllot);
        String[] split = time.split(":");
        return split[1] + "分" + split[2] + "秒";
    }

    /**
     * 毫秒转换成分钟
     *
     * @param millisecond
     * @return
     */
    public static String changeMillseTime(String millisecond) {
        if (StringUtils.isBlank(millisecond)) {
            return null;
        }
        long speedAllot = Math.round(Double.valueOf(millisecond));
        String time = millisecondToTime(speedAllot);
        String[] split = time.split(":");
        return split[1];
    }

}
